﻿//CÓDIGO ABAIXO ESTÁ APENAS PARA EXEMPLO. 
var menuUrl = "Home.aspx";

window.onload = function () {
    //Verifica se existe algum Cookie
    var dsEmail = readCookie("EmailUsuario");

    document.getElementById("btnLogarEmail").onclick = LoginUser;

    if (dsEmail != null) {
        $(window.document.location).attr('href', menuUrl);
    }

};


function LoginUser() {
    var dsEmail = document.getElementById("inputEmail").value;


    if (validaEmail(dsEmail)) {
        $.ajax({
            type: "POST",
            url: "Default.aspx/CreateUser",
            data: "{'email':'" + dsEmail + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: true,
            cache: false,
            success: function (msg) {
                writeCookie("EmailUsuario", dsEmail, 1);
                $(window.document.location).attr('href', menuUrl);
                return true;
            }

        });
    }
}


function validaEmail(dsEmail){
var er = RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
       if(dsEmail == ""){
          window.alert("Informe o seu e-mail!");
          return false;
       }else if(er.test(dsEmail) == false){
          window.alert("E-mail inválido!");
          return false;
       }    else {
              return true;
           }

    }
